<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 16:43
 */

namespace plugin\admin\app\controller\set;


use plugin\admin\app\controller\AuthController;
use support\Redis;
use support\Request;
use support\Response;

class IndexController extends AuthController
{
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('set/index/index');
    }

    public function getParamSetting(Request $request)
    {
        $paramAllConfig = config('param');

        if (!isset($paramAllConfig['customer_service_qrcode'])) {
            $paramAllConfig += include(runtime_path('config/param.php'));
        }

        return json(['code' => 0, 'data' => $paramAllConfig, 'msg' => 'ok']);
    }

    public function saveParamSetting(Request $request)
    {
            $param['first_input'] = $request->input('first_input');
            $param['mutil_textarea'] = $request->input('mutil_textarea');
            $param['mutil_input'] = $request->input('mutil_input');

            $string = "<?php
  /**
   * 系统通用参数配置
   * User: xp
   * Date: 2021/2/5
   * Time: 10:52
   */

  return [\n";

            if(!empty($param['first_input'])){
                foreach($param['first_input'] as $val){
                    $string .= "     '{$val['name']}'=>'".str_replace('\'','\\\'',$val['value'])."', \n";
                }
            }


            if(!empty($param['mutil_textarea'])){
                foreach($param['mutil_textarea'] as $val){
                    $val['value'] = array_filter($val['value']);

                    $string .= "     '{$val['name']}'=>'".implode("|@@|",str_replace('\'','\\\'',$val['value']))."|@@|',\n";
//                    $string .= "     '{$val['name']}'=>[ \n";
//                    foreach($val['value'] as $v){
//                        if($v===''){
//                            continue;
//                        }
//                        $string .= "           '{$v}', \n";
//                    }
//                    $string .= "      ], \n";
//                    if($val['name']=='ai_command_blacklist_works'){
//                        var_dump($string);
//                    }
                }
            }


            $mutilInputData = [];
            if(!empty($param['mutil_input'])){
                foreach($param['mutil_input'] as $val){
                    $names = array_filter(explode('.',$val['name']));
                    switch (count($names)){
                        case 2:
                            $mutilInputData[$names[0]][$names[1]] = str_replace('\'','\\\'',$val['value']);
                            break;
                        case 3:
                            $mutilInputData[$names[0]][$names[1]][$names[2]] = str_replace('\'','\\\'',$val['value']);
                            break;
                        case 4:
                            $mutilInputData[$names[0]][$names[1]][$names[2]][$names[3]] = str_replace('\'','\\\'',$val['value']);
                            break;
                    }
                }
            }

            foreach($mutilInputData as $key=>$val){
                $string .= "      '{$key}'=>[ \n";
                foreach($val as $k=>$v){
                    if(is_array($v)){
                        $string .= "          '{$k}'=>[ \n";
                        foreach($v as $kk=>$vv){
                            if(is_array($vv)){
                                $string .= "              '{$kk}'=>[ \n";
                                foreach($vv as $kkk=>$vvv){
                                    $string .= "                       '{$kkk}'=>'{$vvv}', \n";
                                }
                                $string .= "            ], \n";
                            }else{
                                $string .= "              '{$kk}'=>'{$vv}', \n";
                            }
                        }
                        $string .= "             ], \n";
                    }else{
                        $string .= "      '{$k}'=>'{$v}', \n";
                    }
                }
                $string .= "          ], \n";
            }
            $string .= "     ];";

            file_put_contents(runtime_path('config/param.php'), $string);

            Redis::set('config_param_data',$string);

            return json(['code' => 0, 'data' => [], 'msg' => 'ok']);
    }
}